<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use app\portal\model\SeriesModel;
use cmf\controller\AdminBaseController;
use think\Db;
use think\db\Query;
use app\portal\model\PortalPostModel;

class AdminSeriesController extends AdminBaseController
{
    //产品系列


    //列表
    public function index(){
        $param = $this->request->param();
        $res = Db::name('series')
            ->alias('s')
            ->join('type t','s.t_id = t.id')
            ->where(function (Query $query) use ($param) {
                $title = empty($param['title']) ? '' : $param['title'];
                if (!empty($title)) {
                    $query->where('s.title', 'like', "%$title%");
                }
                //分类
                $type_id = empty($param['type_id']) ? '' : $param['type_id'];
                if (!empty($type_id)) {
                    $query->where('s.t_id', $type_id);
                }
            })
            ->field('s.id,s.title,s.thumbnail,s.create_time,t.name')
            ->order('s.id desc')
            ->paginate(10);
        $data = $res->toArray();
        $data = $data['data'];
        foreach ($data as &$value){
            $value['name'] = '&nbsp;&nbsp;&nbsp;├─'.$value['name'];
        }
        $page = $res->render();

        $this->assign('list',$data);
        $this->assign('page',$page);
        $this->assign('title',isset($param['title']) ? $param['title'] : '');
        $this->assign('type_id',isset($param['type_id']) ? $param['type_id'] : '');

        //搜索分类
        $select_category = AdminSeriesTypeController::getList();
        $this->assign('select_category',$select_category);
        return $this->fetch();
    }

    //添加页面
    public function add(){
        return $this->fetch();
    }

    //添加提交
    public function addPost(){
        $data = $this->request->param();
        $result = $this->validate($data, 'AdminSeries.edit');
        if ($result !== true) {
            $this->error($result);
        }

        $seriesModel = new SeriesModel();
        $res = $seriesModel->create($data);
        if($res){
            $this->success('添加成功!', url('AdminSeries/index'));
        }else{
            $this->error('失败');
        }
    }

    //编辑页面
    public function edit(){
        $id = $this->request->param('id');
        $post = Db::name('series')
            ->where(['id'=>$id])
            ->find();
        $contentModel = new PortalPostModel();
        $post['detail'] = $contentModel->getPostContentAttr($post['detail']);
        $post['detail_en'] = $contentModel->getPostContentAttr($post['detail_en']);

        //查询分类名称
        $type = Db::name('type')->where(['id'=>$post['t_id']])->find();
        if($type){
            $post['children_name'] = $type['name'];
        }
        $this->assign('post',$post);
        return $this->fetch();
    }

    //编辑提交
    public function editPost(){
        $data = $this->request->param();
        $result = $this->validate($data, 'AdminSeries.edit');
        if ($result !== true) {
            $this->error($result);
        }

        $seriesModel = new SeriesModel();
        // 显式指定更新数据操作
        $res = $seriesModel->isUpdate(true)->save($data);
        if($res){
            $this->success('保存成功!', url('AdminSeries/index'));
        }else{
            $this->error('失败');
        }
    }

    //删除
    public function delete(){
        $id = $this->request->param('id');
        $seriesModel = new SeriesModel();
        $res = $seriesModel->where(['id'=>$id])->delete();
        if($res){
            $this->success('删除成功!');
        }else{
            $this->error('失败');
        }
    }
}
